package com.mybatiscore.jiaocheng.d01插入数据时获取自动生成的吧主键;

import com.mybatiscore.Mapper.TRoleMapper;
import com.mybatiscore.Mapper.TUserMapper;
import com.mybatiscore.POJO.TRole;
import com.mybatiscore.POJO.TUser;
import com.mybatiscore.Util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class d01_插入数据时获取自动生成的吧主键 {
    @Test
    public void testUseGeneratedKeys1(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        TUserMapper mapper1 = sqlSession.getMapper(TUserMapper.class);
        TRoleMapper mapper2 = sqlSession.getMapper(TRoleMapper.class);
//        useGeneratedKeys="true"：使用自动生成的主键值
//        keyProperty="id"：将生成的值指定赋值给该对象的属性
        TUser t1 = new TUser(null, "小明");
        int id1 = mapper1.insertUserUseGeneratedKeys(t1);
        mapper2.insertRole(new TRole(null,"项目经理",t1.getId()));
        mapper2.insertRole(new TRole(null,"技术总监",t1.getId()));
        mapper2.insertRole(new TRole(null,"java软件工程师",t1.getId()));
        System.out.println(t1.getId());
        System.out.println(t1);

        TUser t2 = new TUser(null, "小花");
        int id2 = mapper1.insertUserUseGeneratedKeys(t2);
        System.out.println(t2);
        System.out.println(id2);
        mapper2.insertRole(new TRole(null,"CEO",t2.getId()));
        mapper2.insertRole(new TRole(null,"组长",t2.getId()));
        mapper2.insertRole(new TRole(null,"测试员",t2.getId()));
        sqlSession.commit();
        sqlSession.close();
    }
}
